package edu.chl.tda367.mynotes.ctrl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import edu.chl.tda367.mynotes.core.MyNotes;
import edu.chl.tda367.mynotes.io.FileHandler;
import edu.chl.tda367.mynotes.io.IFileHandler;

/*
 * Takes care of what happens when a document is saved.
 */

public class SaveDocCtrl implements ICtrl, ActionListener {

	private MyNotes mynotes = MyNotes.getInstance();
	private IFileHandler fileHandler = FileHandler.newInstance();

	@Override
	public void actionPerformed(ActionEvent e) {
		execute();
	}

	/**
	 *  If the document already is saved, the document should be saved to
	 *  the same file. If the document isn't saved, choose a place to save it.
	 */
	
	@Override
	public void execute() {

		if(mynotes.getIsSaved() == false){
			new SaveDocAsCtrl().execute();
		} else {
			File file = new File(MyNotes.getInstance().getFilePath());
			fileHandler.saveFile(file, MyNotes.getInstance().getDocument());
			mynotes.setIsSaved(true);
			mynotes.setSavedDocument();
		}
	}

	@Override
	public void execute(String s) {
		
	}
}
